package domain.searchEngine.origin;

import java.util.Date ;
import java.util.Vector ;

import persistence.OriginIndex ;
import domain.message.Post ;
import domain.searchEngine.IndexAgent ;
import domain.user.Login ;



/**
 * The Class IndexAgent.
 */
public class OriginIndexAgent implements IndexAgent
{

	/** The index field. */
	private OriginIndex index;

	/** The global word id which increases by 1 with every new word. */
	private static long globalWordId = 0 ;



	/**
	 * Instantiates a new index agent.
	 *
	 * @param i the index
	 */
	public OriginIndexAgent(final OriginIndex i)
	{
		this.index = i;
	}

	/**
	 * @see domain.searchEngine.IndexAgent#addNewLoginToIndex(domain.user.Login)
	 */
	@Override
	public void addNewLoginToIndex(final Login login)
	{
		this.index.putLogin(login.getId(), login.getTime()) ;
	}

	/**
	 * Adds the new post info to the data structures of index.
	 * separates the message into words and updates the  data structures of index accordingly.
	 * @param post the post
	 * @param userName the user name of the author of the message
	 *
	 */
	public void addNewPostToIndex(final Post post,final String userName)
	{
		Long userId = post.getWriterId();
		Date date = post.getDate();
		String currWord;
		Vector<String> seperatedWords = this.separate(post.getMessage());
		for(int i=0;i<seperatedWords.size();i++)
		{
			currWord = seperatedWords.get(i);
			long wordId = this.updateIndexWithWord(currWord);
			this.index.putMessageInSetOfMessagesForWord(wordId, post.getId());
		}
		this.updateIndexWithPostMetadata(post, userName, userId, date);
	}

	/**
	 * removes a post info from the data structures of index
	 * @param post the post to remove
	 * @param userName the writer of this post
	 */
	public void removePostFromIndex(final Post post,final String userName)
	{
		//TODO
	}


	/**
	 * Separate the message into words.
	 *
	 * @param message the message
	 *
	 * @return the vector< string> of all the separated words.
	 */
	private Vector<String> separate(final String message)
	{
		Vector<String> separatedWords = new Vector<String>();
		String[] arrayOfWords = message.split(" ");
		for (String arrayOfWord: arrayOfWords)
		{
			separatedWords.add(arrayOfWord);
		}
		return separatedWords;
	}

	/**
	 * Rest of actions - inner function which sets the data structures of index.
	 *
	 * @param post the post
	 * @param userName the user name
	 * @param userId the user id
	 * @param date the date
	 */
	private void updateIndexWithPostMetadata(final Post post,final String userName,final Long userId,final Date date)
	{
		this.index.putMessageByContext(post.getMessage(), post.getId());
		this.index.putAuthorByUserName(userName, userId);
		this.index.putMessageInSetOfMessageForDate(date, post.getId());
		this.index.putMessageInSetOfMessageForAuthor(userId, post.getId());
	}

	/**
	 * Update index.
	 *
	 * @param currWord the curr word
	 *
	 */
	private long updateIndexWithWord(final String currWord)
	{
		Long currWordId;
		if (!this.index.containsWord(currWord))
		{
			OriginIndexAgent.globalWordId++;
			currWordId = OriginIndexAgent.globalWordId;
			this.index.putWordByName(currWord, currWordId);
		}
		else
		{
			currWordId = this.index.getWordsByName(currWord);
		}
		return currWordId ;
	}
}
